<?php
/* $Id$ */
/*
	multiwan.php

	Copyright (C) 2008 Ermal Lu�i
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

require("guiconfig.inc");

if (!is_array($config['multiwan']))
	$config['multiwan'] = array();

$a_multiwan = &$config['multiwan'] ;

$if_list = get_configured_interface_with_descr();

if ($_POST) {
    if($_POST['apply'])
    {
        
        $retval = filter_configure();
        
//        require_once('squid.inc');
//        squid_resync(false);

        if(file_exists($d_multiwandirty_path))
        {
            unlink($d_multiwandirty_path);
        }
        
        $savemsg = "设置已被应用，防火墙规则正在后台重新载入。您可以<a href='status_filter_reload.php'>查看</a>重载进度。";
    }
    else
    {
        if(in_array('lan', $_POST['iface_array']))
        {
            $input_error[] = "LAN不能作为外网网卡。";
        }
        if(!in_array('wan', $_POST['iface_array']))
        {
            $input_error[] = "WAN必须是外网网卡之一。";
        }
        foreach($_POST['iface_array'] as $if)
        {
            if(!isset($if_list[$if]))
            {
                $input_error[] = "无效接口。";
            }
        }
        
        if(!$input_error)
        {
            if(count($_POST['iface_array']) < 2)
            {
                unset($config['multiwan']);
                unset($a_multiwan['if']);
            }
            else
            {
                $a_multiwan['if'] = implode(',', $_POST['iface_array']);
            }
            
            write_config();
            
            touch($d_multiwandirty_path);
        }
    }
}

$selected_if = explode(',', $a_multiwan['if']);

$pgtitle = array("网络设置","多WAN设置（负载均衡）");
include("head.inc");

?>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<?php if ($input_errors) print_input_errors($input_errors); ?>
<div class="soft_cont_right_table">
<form method="post" action="multiwan.php">
<?php if ($savemsg) print_info_box($savemsg); ?>
<?php if (file_exists($d_multiwandirty_path)): ?><p>
<?php print_info_box_np("多WAN口设置已更改。<br>请应用更改使其生效。");?><br>
<?php endif; ?>

	<table width="100%" border="0" cellpadding="6" cellspacing="0">
	<tr><td class="listtopic" colspan="2">多WAN口负载均衡设置</td></tr>
    <tr valign="top">
        <td width="22%" class="vncell">接口</td>
        <td class="vtable">
        <select id='iface_array' name="iface_array[]" size="3" multiple>
        <?php foreach($if_list as $name => $descr): ?>
            <option value="<?=$name;?>" <?php if(in_array($name, $selected_if) || $name == "wan") echo "selected"; ?> ><?=$descr;?></option>
        <?php endforeach; ?>
        </select>
        <br />您可以使用CTRL键进行多个选择。
        </td>
    </tr>
    <tr><td></td></tr>
    <tr>
        <td width="22%" valign="top">&nbsp;</td>
        <td width="78%">
            <input name="Submit" type="submit" class="formbtn" value="保存">
        </td>
    </tr>
    </table>
    </form>
  </div>
<?php include("fend.inc"); ?>
</body>
</html>
